#include "ComponentRegistry.h"

CGlobalComponentRegistry& CGlobalComponentRegistry::GetInstance()
{
    static CGlobalComponentRegistry instance;
    return instance;
}

bool CGlobalComponentRegistry::RegisterComponent(const std::shared_ptr<CModelComponent>& component)
{
    if (!component || !component->Validate()) {
        return false;
    }
    
    wxString name = component->GetMetadata().name;
    m_components[name] = component;
    return true;
}

bool CGlobalComponentRegistry::UnregisterComponent(const wxString& componentName)
{
    return m_components.erase(componentName) > 0;
}

std::shared_ptr<CModelComponent> CGlobalComponentRegistry::GetComponent(const wxString& componentName)
{
    auto it = m_components.find(componentName);
    if (it != m_components.end()) {
        return it->second;
    }
    return nullptr;
}

std::vector<std::shared_ptr<CModelComponent>> CGlobalComponentRegistry::SearchComponents(
    const wxString& keyword, const wxString& category)
{
    std::vector<std::shared_ptr<CModelComponent>> results;
    
    for (const auto& pair : m_components) {
        const auto& metadata = pair.second->GetMetadata();
        
        if (metadata.name.Contains(keyword) ||
            metadata.description.Contains(keyword) ||
            metadata.author.Contains(keyword)) {
            results.push_back(pair.second);
        }
    }
    
    return results;
}

std::vector<std::shared_ptr<CModelComponent>> CGlobalComponentRegistry::GetComponentsByAuthor(
    const wxString& authorId)
{
    std::vector<std::shared_ptr<CModelComponent>> results;
    
    for (const auto& pair : m_components) {
        if (pair.second->GetMetadata().author == authorId) {
            results.push_back(pair.second);
        }
    }
    
    return results;
}

size_t CGlobalComponentRegistry::GetTotalComponentCount() const
{
    return m_components.size();
}

std::map<wxString, size_t> CGlobalComponentRegistry::GetComponentStatistics() const
{
    std::map<wxString, size_t> stats;
    stats["Total Components"] = m_components.size();
    return stats;
}
